  $(function() {
    //add a tag at the carrot
   	$.fn.addTagType = function (elemID, tagType)
	{
		var elem = document.getElementById(elemID);
		var text = elem.value;
		var pos =  $("#noteArea").caret();
		text = text.slice(0, pos)+tagType+text.slice(pos);
		elem.value=text;
		elem.focus();
		 $("#noteArea").caret(pos+1);
	}
    
    //split the text box into array of words, spaces, and tags
    function split( val ) {
    	//alert(val.match(/#\w*|\w*\s/g));
      return val.match(/[#|@|^|*]\w*|\w*\s|\w*/g);
    }
    
    //get the last term
    function extractLast( term ) {
      return split( term ).pop();
    }
    
    //get the term at the cursor
    function extractCurrentWord(term, pos){
    	//get the term divided into words
    	var words = split(term);
    	//get word index
    	var index = extractWordIndex(words, pos);
    	return words[index];    	
    }
    
    //get the term at the cursor
    function extractWordIndex(words, pos){
    	var countTotal = 0;
    	//check each word
    	for (var i = 0; i < words.length; i++ )
    	{
    		//get the lenght of the word
    		var wordLength = words[i].length;
    		//alert(wordLength);
    		//if the lenght of this word plus previous words is less than position add to previous lenght
    		if(countTotal + wordLength < pos){
    			countTotal = countTotal + wordLength;
    		}else{
    			//other wise return the word
    			//alert("the word:"+words[i]);
    			return i;
    		}    		
    	}
    }
    
    $( "#noteArea" )
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).data( "ui-autocomplete" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        minLength: 0,

        source: function( request, response ) {
      	  var pos = $("#noteArea").caret();
          var term = extractCurrentWord( request.term, pos );
          if(term.match(/[#|@|^|*]\w*/)){
            $.getJSON( "autoCompleteSearch.php", {
              term: term
            }, response );
          }
        },
        
//        source: function( request, response ) {
//          // delegate back to autocomplete, but extract the last term
//          var pos = $("#noteArea").caret();
//          var term = extractCurrentWord( request.term, pos );
//          alert("the pos:"+pos);
//          //alert("the term:"+term);
//          //only start autocomplete for tags
//          if(term.match(/[#|@|^|*]\w*/)){
//        	  alert("the term:"+term);
//          	response( $.ui.autocomplete.filter(
//          			availableTag, term ) );
//          }
//        },
        
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          var pos = $(this).caret();
          var index = extractWordIndex(terms, pos);
          // remove the current input
          // add the selected item
          terms.splice(index, 1, ui.item.value );
          
          var finalPos = 0;
          for (var i = 0; i <= index; i++ ){
        	  finalPos = finalPos + terms[i].length;
          }
          // add placeholder to get the comma-and-space at the end
          //terms.push( "" );
          this.value = terms.join( "" );
          $(this).caret(finalPos);
          searchSub();
          return false;
        }
      });
  })